package com.sz.pc.aop;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.feilong.core.Validator;
import com.sz.pc.entity.model.constants.Constants;
import com.sz.pc.entity.tb.SysLoginLog;
import com.sz.pc.service.interfs.log.ISysLoginLogService;
import com.sz.pc.utils.ServletUtil;

@Aspect
@Component
public class LoginAop {

    private Logger logger = LoggerFactory.getLogger(LoginAop.class);

    @Resource
    private ISysLoginLogService loginLogService;

    @Pointcut(value="execution(* com.sz.pc.controller.base.IndexController.doLogin(..))))")
    private void loginLog() {
    }

    @Before("loginLog()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        logger.info("URL : " + request.getRequestURL().toString()+"HTTP_METHOD : " + request.getMethod()+"IP : "
        + request.getRemoteAddr()+"CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." +
        		joinPoint.getSignature().getName()+"ARGS : " + Arrays.toString(joinPoint.getArgs()));
       
    }

    @After("loginLog()")
    public void doAfter() {
    	Integer userId = (Integer) ServletUtil.getSession().getAttribute(Constants.CURRENT_USER_ID);
    	String msg = (String) ServletUtil.getRequest().getAttribute("msg");
        String terminal = ServletUtil.getTerminal();
        if(Validator.isNullOrEmpty(userId)){
            return;
        }
        if(Validator.isNotNullOrEmpty(msg)){
            this.writeLoginLog(LoginStatus.EXCEPTION.getStatus(),msg,userId,terminal);
            return;
        }
        this.writeLoginLog(LoginStatus.NORMAL.getStatus(),null,userId,terminal);
    }

    private void writeLoginLog(String status,String msg,Integer userId,String terminal) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setSysId(userId);
        loginLog.setStatus(status);
        loginLog.setLoginTime(new Date());
        loginLog.setLoginIp(ServletUtil.getIpAddr());
        if(Validator.isNotNullOrEmpty(msg)){
            loginLog.setRemark(msg);
        }
        loginLogService.insert(loginLog);
    }

    private enum LoginStatus {
        NORMAL("登录成功"), EXCEPTION("登录失败");
        private String status;

        LoginStatus(String status) {
            this.status = status;
        }

        public String getStatus() {
            return status;
        }
    }

}
